import time

from selenium.webdriver.common.by import By
from common.Utills import BlogDriver
#测试博客登录页面

class BlogLogin:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_login.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)
    #成功登录的测试用例
    def LoginSucTest(self):
        # 清空账号框
        self.driver.find_element(By.CSS_SELECTOR, '#username').clear()
        # 清空密码框
        self.driver.find_element(By.CSS_SELECTOR, '#password').clear()
        #输入正确的账号
        self.driver.find_element(By.CSS_SELECTOR,'#username').send_keys("zhangsan")
        #输入正确的密码
        self.driver.find_element(By.CSS_SELECTOR,'#password').send_keys("123456")
        #点击确认
        self.driver.find_element(By.CSS_SELECTOR,'#submit').click()
        #能够找到博客首页用户的昵称。说明登录成功，否则登录失败
        self.driver.find_element(By.CSS_SELECTOR,'body > div.container > div.left > div > h3')
        # 添加一个屏幕截图
        BlogDriver.getScreeShont()
        #返回登录页面
        # self.driver.back()
    #异常登录的测试用例
    #正确的账号和错误的密码
    def LoginFailTest1(self):
        #若连续多次的send_keys则会出现关键词拼接，而不是替换，若要替换需要先clear
        #清空账号框
        self.driver.find_element(By.CSS_SELECTOR,'#username').clear()
        #正确的账号
        self.driver.find_element(By.CSS_SELECTOR, '#username').send_keys("zhangsan")
        #清空密码框
        self.driver.find_element(By.CSS_SELECTOR,'#password').clear()
        #错误的密码
        self.driver.find_element(By.CSS_SELECTOR, '#password').send_keys("12345")

        #点击登录
        self.driver.find_element(By.CSS_SELECTOR, '#submit').click()
        time.sleep(3)
        # 获取弹窗信息
        alert = self.driver.switch_to.alert
        #断言检测一下是否符合预期
        assert alert.text == '密码错误'
        #确认弹窗
        alert.accept()
        # 添加一个屏幕截图
        BlogDriver.getScreeShont()
        # self.driver.quit()
    #错误的账号和正确的密码
    def LoginFailTest2(self):
        #清空账号框
        self.driver.find_element(By.CSS_SELECTOR,'#username').clear()
        # 错误的账号
        self.driver.find_element(By.CSS_SELECTOR,'#username').send_keys('lis')
        #清空密码框
        self.driver.find_element(By.CSS_SELECTOR,'#password').clear()
        #正确的密码
        self.driver.find_element(By.CSS_SELECTOR,'#password').send_keys('123456')
        #点击登录
        self.driver.find_element(By.CSS_SELECTOR, '#submit').click()
        time.sleep(3)
        #获取弹窗信息
        alert = self.driver.switch_to.alert
        # 断言检测一下是否符合预期
        assert alert.text == '用户不存在'
        # 确认弹窗
        alert.accept()
        # 添加一个屏幕截图
        BlogDriver.getScreeShont()
    #错误的账号和错误的密码
    def LoginFailTest3(self):
        #清空账号框
        self.driver.find_element(By.CSS_SELECTOR,'#username').clear()
        #输入错误的账号
        self.driver.find_element(By.CSS_SELECTOR,'#username').send_keys('lis')
        #清空密码框
        self.driver.find_element(By.CSS_SELECTOR,'#password').clear()
        #输入错误的密码
        self.driver.find_element(By.CSS_SELECTOR,'#password').send_keys('12345')
        #点击登录
        self.driver.find_element(By.CSS_SELECTOR,'#submit').click()
        time.sleep(3)
        #获取弹窗信息
        alert = self.driver.switch_to.alert
        # 断言检测一下是否符合预期
        assert alert.text == '用户不存在'
        # 确认弹窗
        alert.accept()
        # 添加一个屏幕截图
        BlogDriver.getScreeShont()


# login = BlogLogin()
#
# login.LoginFailTest1()
# login.LoginFailTest2()
# login.LoginFailTest3()
# login.LoginSucTest()
